﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Lib;
using DVSProject.Models;

namespace DVSProject.Controllers
{
    public class MerchantController : Controller
    {
        //
        // GET: /Merchant/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MerchantRepo _MerchantRepo = new MerchantRepo();
            IEnumerable<MerchantModels> _Merchants = _MerchantRepo.GetMerchant();
            return View(_Merchants);

        }

        //
        // GET: /Merchant/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MerchantRepo _MerchantRepo = new MerchantRepo();
            MerchantModels _MerchantModels = new MerchantModels();
            _MerchantModels = _MerchantRepo.GetMerchantRemarkById(id);
            return View(_MerchantModels);
        }

        //
        // GET: /Merchant/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            return View();
        } 

        //
        // POST: /Merchant/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                MerchantRepo _MerchantRepo = new MerchantRepo();
                MerchantModels _Merchant= new MerchantModels();

                _Merchant.mcode = collection["mcode"];
                _Merchant.NameTh = collection["NameTh"];
                _Merchant.NameEng = collection["NameEng"];
                _Merchant.AddressTh = collection["AddressTh"];
                _Merchant.AddressEng = collection["AddressEng"];
                _Merchant.Tel = collection["Tel"];
                _Merchant.Fax = collection["Fax"];
                _Merchant.CreateBy = (string)Session["User"];

                bool result = _MerchantRepo.Create(_Merchant);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Merchant/Edit/5

        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MerchantRepo _MerchantRepo = new MerchantRepo();
            MerchantModels _Merchant = new MerchantModels();
            _Merchant = _MerchantRepo.GetMerchantRemarkById(id);

            return View(_Merchant);
        }

        //
        // POST: /Merchant/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here
                MerchantRepo _MerchantRepo = new MerchantRepo();
                MerchantModels _Merchant = new MerchantModels();
                _Merchant.MerchantId = id;
                _Merchant.mcode = collection["mcode"];
                _Merchant.NameTh = collection["NameTh"];
                _Merchant.NameEng = collection["NameEng"];
                _Merchant.AddressTh = collection["AddressTh"];
                _Merchant.AddressEng = collection["AddressEng"];
                _Merchant.Tel = collection["Tel"];
                _Merchant.Fax = collection["Fax"];
                _Merchant.UpdateBy = (string)Session["User"];
                bool result = _MerchantRepo.Edit(_Merchant);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Merchant/Delete/5

        public ActionResult Delete(int MerchantId)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MerchantRepo _MerchantRepo = new MerchantRepo();
            MerchantModels _Merchant = new MerchantModels();
            _Merchant = _MerchantRepo.GetMerchantRemarkById(MerchantId);
            return View(_Merchant);
        }

        //
        // POST: /Merchant/Delete/5

        [HttpPost]
        public ActionResult Delete(int MerchantId, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                MerchantRepo _MerchantRepo = new MerchantRepo();
                MerchantModels _Merchant = new MerchantModels();
                _Merchant.MerchantId = MerchantId;
                _Merchant.UpdateBy = (string)Session["User"];
                bool result = _MerchantRepo.Delete(_Merchant);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
